import { successResponse, errorResponse } from 'server/utils/response'
import { Projects } from 'server/models/Project'

export default defineEventHandler(async (event) => {
  try {
    const { id } = getRouterParams(event)
    
    if (!id) {
      return errorResponse('项目ID不能为空')
    }

    const projectId = parseInt(id)
    if (isNaN(projectId)) {
      return errorResponse('项目ID格式不正确')
    }

    // 获取当前项目
    const project = await Projects.getProjectById(projectId)
    if (!project) {
      return errorResponse('项目不存在')
    }

    // 增加浏览量
    await Projects.updateProject(projectId, {
      view_count: (project.view_count || 0) + 1
    })

    return successResponse(null, '记录浏览量成功')
  } catch (error) {
    console.error('记录浏览量失败:', error)
    return errorResponse('记录浏览量失败', error instanceof Error ? error.message : '未知错误')
  }
})